package com.googlecode.dgwt.client.dijit;

import com.google.gwt.dom.client.Element;
import com.google.gwt.event.shared.GwtEvent;
import com.googlecode.dgwt.client.dijit.base.widget.WidgetEngine;
import com.googlecode.dgwt.client.dijit.theme.CSS;
import com.googlecode.dgwt.client.dijit.util.Template;
import com.googlecode.dgwt.client.dojo.URL;

/**
 * @author wellsantos@wellsantos.com
 * @since 0.01
 */
public class Composite extends Container {
    
    protected CSS styleSheet = CSS.EMPTY_CSS;
    protected Template template;

    protected Composite(boolean ready) {
        super(ready);
    }
    
    public Composite(URL url) {
        this(new Template(url));
    }
    
    public Composite(String html) {
        super(false);
        template = HTML.loadFromHTML(this, html, "div");
        postCreate();
    }

    public Composite(Element element) {
        this(new Template(element));
    }
    
    public Composite(Template template) {
        super(false);
        this.template = template;
        loadFromTemplate(template);
        postCreate();
    }

    public HTML bindAttachmentAsHTMLWidget(final String name){
        View view = findView(name);
        Widget widget = view == null ? null : view.getWidget();
        
        if (widget != null){
            assert (widget instanceof HTML) : "Attachment was previous binded as Container Widget";
            return (HTML) widget;
        }
        
        Element element = getAttachedElement(name);
        
        if (element != null 
                && !element.equals(getElement())){
            
            final HTML attachment = new HTML(false);
            attachment.setDomElement(element);
            WidgetEngine.get(attachment).postCreate();
            addView(new View(){

                @Override
                public String getName() {
                    return name;
                }

                @Override
                public Widget getWidget() {
                    return attachment;
                }

                @Override
                public void fireEvent(GwtEvent<?> event) {
                    getWidget().fireEvent(event);
                }                
            });
            return attachment;
        }
        return null;
    }

    public Container bindAttachmentAsContainer(final String name){
        View view = findView(name);
        Widget widget = view == null ? null : view.getWidget();
        
        if (widget != null){
            assert (widget instanceof Container) : "Attachment was previous binded as HTML Widget";
            return (Container) widget;
        }
        
        Element element = getAttachedElement(name);
        
        if (element != null 
                && !element.equals(getElement())){
            
            final Container attachment = new Container(false);
            attachment.setDomElement(element);
            WidgetEngine.get(attachment).postCreate();
            addView(new View(){

                @Override
                public String getName() {
                    return name;
                }

                @Override
                public Widget getWidget() {
                    return attachment;
                }                

                @Override
                public void fireEvent(GwtEvent<?> event) {
                    getWidget().fireEvent(event);
                }                
            });

            return attachment;
        }
        return null;
    }

    public Template getTemplate(){
        if (template == null) {
            template = new Template(getElement());
        }
        return template;
    }
    
    @Override
    public CSS getThemeCSS() {
        return styleSheet;
    }
    
    public void setStyleSheet(CSS css){
        
        assert (css != null) : "CSS can't be NULL";
        assert (styleSheet.equals(CSS.EMPTY_CSS)) : "CSS can only be set once";
        
        styleSheet = css;
        styleSheet.applyTo(this, true);
    }
        
}
